package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FreezeInfoApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.domain.FreezeInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {
    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FreezeInfoApi freezeInfoApi;

    //评论列表
    public PageResult findComments(String movementId, Integer page, Integer pageSize) {
        Integer commentType = 2;//设置互动类型（评论）
        //1. 调用接口，获取互动信息
        List<Comment> commentList = commentApi.findComments(movementId, commentType, page, pageSize);
        //2. 判断是否为空
        if (CollUtil.isEmpty(commentList)) {
            return new PageResult();
        }
        //2.1 获取所有评论列表里的评论人
        List<Long> ids = CollUtil.getFieldValues(commentList, "userId", Long.class);
        //2.2 调用API一次性查询所有评论人的信息
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        //3. 循环集合，一个Comment构建一个vo
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : commentList) {
            //3.1 获取互动的操作人,获取互动操作人的信息
            UserInfo userInfo = map.get(comment.getUserId());
            CommentVo vo = CommentVo.init(userInfo, comment);
            vos.add(vo);
        }
        //4.返回
        return new PageResult(page, pageSize, 0l, vos);
    }

    //更新互动表
    public void saveComment(String content, String movementId) {
        Long userId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId);
        if (userInfo.getUserStatus().equals("2")) {
            //根据userId查询冻结信息
            FreezeInfo freezeInfo = freezeInfoApi.findByUserId(userId);
            if (freezeInfo.getFreezingRange() == 2) {
                throw new BusinessException(new ErrorResult("99999","你已被禁止发布评论"));
            }
        }
        Integer commentType = 2;
        //1. 创建comment对象，设置属性值
        Comment comment = new Comment();
        comment.setCommentType(commentType);
        comment.setContent(content);
        comment.setPublishId(new ObjectId(movementId));
        comment.setCreated(System.currentTimeMillis());
        comment.setUserId(UserHolder.getUserId());
        //2.调用api保存（保存之后，返回评论总数）
        Integer count = commentApi.save(comment);
        System.out.println("评论总数：" + count);

    }
}
